home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / checkbox / plugins / launchpad_exchange.py < prev    next >
Encoding:
Python Source  |  2009-04-27  |  5.1 KB  |  151 lines

  1. #
  2. # This file is part of Checkbox.
  3. #
  4. # Copyright 2008 Canonical Ltd.
  5. #
  6. # Checkbox is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Checkbox is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import time
  20. import pprint
  21. import bz2
  22. import logging
  23. import posixpath
  24.  
  25. from gettext import gettext as _
  26. from socket import gethostname
  27. from StringIO import StringIO
  28.  
  29. from checkbox.lib.conversion import string_to_type
  30. from checkbox.lib.log import format_delta
  31. from checkbox.lib.transport import HTTPTransport
  32.  
  33. from checkbox.properties import Int, String
  34. from checkbox.plugin import Plugin
  35.  
  36.  
  37. class LaunchpadExchange(Plugin):
  38.  
  39.     # Timeout value for each submission.
  40.     timeout = Int(default=120)
  41.  
  42.     # URL where to send submissions.
  43.     transport_url = String(default="https://launchpad.net/+hwdb/+submit")
  44.  
  45.     def register(self, manager):
  46.         super(LaunchpadExchange, self).register(manager)
  47.         self._headers = {}
  48.         self._form = {
  49.             "field.private": False,
  50.             "field.contactable": False,
  51.             "field.live_cd": False,
  52.             "field.format": u"VERSION_1",
  53.             "field.actions.upload": u"Upload"}
  54.  
  55.         for (rt, rh) in [
  56.              ("report-architecture", self.report_architecture),
  57.              ("report-client", self.report_client),
  58.              ("report-datetime", self.report_datetime),
  59.              ("report-distribution", self.report_distribution),
  60.              ("report-email", self.report_email),
  61.              ("report-submission_id", self.report_submission_id),
  62.              ("report-system_id", self.report_system_id),
  63.              ("exchange-report", self.exchange_report),
  64.              ("exchange", self.exchange)]:
  65.             self._manager.reactor.call_on(rt, rh)
  66.  
  67.     def report_architecture(self, message):
  68.         self._form["field.architecture"] = message
  69.  
  70.     def report_client(self, message):
  71.         user_agent = "%s/%s" % (message["name"], message["version"])
  72.         self._headers["User-Agent"] = user_agent
  73.  
  74.     def report_datetime(self, message):
  75.         self._form["field.date_created"] = message
  76.  
  77.     def report_distribution(self, message):
  78.         self._form["field.distribution"] = message.distributor_id
  79.         self._form["field.distroseries"] = message.release
  80.  
  81.     def report_email(self, message):
  82.         self._form["field.emailaddress"] = message
  83.  
  84.     def report_submission_id(self, message):
  85.         self._form["field.submission_key"] = message
  86.  
  87.     def report_system_id(self, message):
  88.         self._form["field.system"] = message
  89.  
  90.     def exchange_report(self, message):
  91.         self._report = message
  92.  
  93.     def exchange(self):
  94.         # Encode form data
  95.         form = {}
  96.         for field, value in self._form.items():
  97.             form[field] = str(value).encode("UTF-8")
  98.  
  99.         # Compress and add payload to form
  100.         payload = open(self._report, "r").read()
  101.         compressed_payload = bz2.compress(payload)
  102.         file = StringIO(compressed_payload)
  103.         file.name = "%s.xml.bz2" % str(gethostname())
  104.         file.size = len(compressed_payload)
  105.         form["field.submission_data"] = file
  106.  
  107.         if logging.getLogger().getEffectiveLevel() <= logging.DEBUG:
  108.             logging.debug("Uncompressed payload length: %d", len(payload))
  109.  
  110.         transport = HTTPTransport(self.transport_url)
  111.  
  112.         start_time = time.time()
  113.         response = transport.exchange(form, self._headers,
  114.             timeout=string_to_type(self.timeout))
  115.         end_time = time.time()
  116.  
  117.         if not response:
  118.             self._manager.reactor.fire("exchange-error", _("""\
  119. Failed to contact server. Please try
  120. again or upload the following file name:
  121. %s
  122.  
  123. directly to the system database:
  124. https://launchpad.net/+hwdb/+submit""") % posixpath.abspath(self._report))
  125.             return
  126.         elif response.status != 200:
  127.             self._manager.reactor.fire("exchange-error", _("""\
  128. Failed to upload to server,
  129. please try again later."""))
  130.             return
  131.  
  132.         if logging.getLogger().getEffectiveLevel() <= logging.DEBUG:
  133.             logging.debug("Response headers:\n%s",
  134.                 pprint.pformat(response.getheaders()))
  135.  
  136.         header = response.getheader("x-launchpad-hwdb-submission")
  137.         if not header:
  138.             self._manager.reactor.fire("exchange-error",
  139.                 _("Information not posted to Launchpad."))
  140.         elif "Error" in header:
  141.             # HACK: this should return a useful error message
  142.             self._manager.reactor.fire("exchange-error", header)
  143.             logging.error(header)
  144.         else:
  145.             text = response.read()
  146.             logging.info("Sent %d bytes and received %d bytes in %s.",
  147.                 file.size, len(text), format_delta(end_time - start_time))
  148.  
  149.  
  150. factory = LaunchpadExchange
  151.